package com.assignwork.service.impl;

import com.assignwork.dto.DynamicFormDTO;
import com.assignwork.dto.DynamicFormDataDTO;
import com.assignwork.entity.DynamicForm;
import com.assignwork.mapper.DynamicFormMapper;
import com.assignwork.service.IDynamicFormDataService;
import com.assignwork.service.IDynamicFormService;
import com.assignwork.vo.DynamicFormVO;
import lombok.AllArgsConstructor;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.secure.BladeUser;
import org.springframework.stereotype.Service;

import java.time.LocalDate;

/**
 * 动态表单实现类
 */
@Service
@AllArgsConstructor
public class DynamicFormServiceImpl extends BaseServiceImpl<DynamicFormMapper, DynamicForm> implements IDynamicFormService {

    IDynamicFormDataService dataService;

    @Override
    public boolean saveOrUpdateLeadershipComments(BladeUser bladeUser, DynamicFormDTO input) {
        if (input.getId() == null) {
            DynamicForm one = this.lambdaQuery()
                    .select(DynamicForm::getId)
                    .eq(DynamicForm::getFormDate, input.getFormDate())
                    .eq(DynamicForm::getFormType, input.getFormType())
                    .orderByDesc(DynamicForm::getUpdateTime)
                    .last("limit 1")
                    .one();
            if (one != null) {
                input.setId(one.getId());
            }
        }
        this.saveOrUpdate(input);
        this.dataService.saveOrUpdateDto(input.getId(), input.getData());
        return false;
    }

    @Override
    public DynamicFormVO formData(BladeUser bladeUser, DynamicFormDTO search) {
        return this.baseMapper.formData(search);
    }
}